package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.context.BaseContext;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.MealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class SetMealServiceImpl implements MealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    //新增套餐
    @Override
    public void insert(SetmealDTO setmealDTO) {
        //新增套餐的基础信息
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.insert(setmeal);
        Long id = setmeal.getId();
        //新增套餐和菜品的关系 需要在上面的操作结束之后获得setmeal_id
        //先删掉再添加
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
//            setmealMapper.delete(id);
            setmealDish.setSetmealId(id);
            setmealDishMapper.insert(setmealDish);
        }

    }
    //分页查询
    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),
                setmealPageQueryDTO.getPageSize());
        //调用mapper层查出来的结果一定要用Page来接
        Page<SetmealVO> page=setmealMapper.select(setmealPageQueryDTO);
        //把setmealvo的属性封装完整------categoryname
        for (SetmealVO vo : page) {
            Long categoryId=vo.getCategoryId();
            String categoryName=categoryMapper.getName(categoryId);
            vo.setCategoryName(categoryName);
        }
        PageResult pageResult=new PageResult();
        pageResult.setTotal(page.getTotal());
        pageResult.setRecords(page.getResult());
        return pageResult ;
    }

    //停售和禁售
    @Override
    public void startOrStop(Long status, Integer id) {
        setmealMapper.updateStatus(status,id);
    }
    //更新操作
    @Override
    public void updateMeal(SetmealDTO setmealDTO) {
        //更新setmeal
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.update(setmeal);
        //获得这个id给后面setmeal_dish当setmealId
        Long setmealId = setmeal.getId();

        log.info("更新setmeal完成");
        //更新setmealDishes
        //先把套餐和菜品对应的数据全删，然后再添加
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealId);
            log.info("setmeal_id:{}",setmealDish.getSetmealId());
            setmealDishMapper.delete(setmealDish.getSetmealId());
            setmealDishMapper.insert(setmealDish);
        }


//        setmealDishMapper.update(setmealDishes);
        log.info("更新setmealDishes完成");
    }

    //根据id查询套餐
    @Override
    public SetmealVO getById(Long id) {
        //查询套餐  注意：categoryName需要自己查，根据categoryId查
        Setmeal setmeal=setmealMapper.getById(id);
        Long categoryId=setmeal.getCategoryId();
        //查询套餐和菜的关系 和categoryName
        List<SetmealDish>list=setmealDishMapper.getById(id);
        String name = categoryMapper.getName(categoryId);
        //封装成SetmealVO返回
        SetmealVO setmealVO=new SetmealVO();
        setmealVO.setCategoryName(name);
        BeanUtils.copyProperties(setmeal,setmealVO);
        log.info("{}",list.size());
        setmealVO.setSetmealDishes(list);
        return setmealVO;
    }



    //查询套餐状态
    @Override
    public Integer getStatus(Long id) {
        Integer status = setmealMapper.getStatus(id);
        return status ;
    }
    //根据id删除套餐
    @Override
    public void deleteById(Long id) {
        setmealMapper.deleteMeal(id);
    }



    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
